package com.hl.card.core.shiro.filter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hl.card.common.util.HttpRequestUtil;
import org.apache.shiro.web.filter.AccessControlFilter;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMethod;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

public class CheckTokenFilter extends AccessControlFilter {

    /**
     * 登录地址
     */
    static final String LOGIN_URL = "/login.html";
    /**
     * 修改密码地址
     */
    static final String NEW_PASSWORD_URL = "/login/new_password.html";
    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue)
            throws Exception {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response) throws Exception {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        HttpServletResponse httpResponse= (HttpServletResponse) response;
        if (httpRequest.getMethod().equals(RequestMethod.OPTIONS.name())) {
            httpResponse.setHeader("Access-control-Allow-Origin", "*");
            httpResponse.setHeader("Access-Control-Allow-Methods", httpRequest.getMethod());
            httpResponse.setHeader("Access-Control-Allow-Headers", httpRequest.getHeader("Access-Control-Request-Headers"));
            httpResponse.setStatus(HttpStatus.OK.value());
            return false;
        }
        Map<String,String> map=new HashMap<>();
        String erroCode="1";
        String erroMsg="";
//        Cookie[] cookies = httpRequest.getCookies();
//        List<Cookie> list = Arrays.asList(cookies);
//        for(int i=0;i<cookies.length;i++){
//            if(cookies[i].getName().equals("regCode")){
//            //判断regCode合法性
//                return true;
//            }
//        }
        String token=null;
        String userid=null;
        String token1=httpRequest.getParameter("token");
        String token2=httpRequest.getHeader("token");
//        String userid1=httpRequest.getParameter("userid");
//        String userid2=httpRequest.getHeader("userid");
        if(null!=token1){
            token=token1;
//            userid=userid1;
        }else{
            token=token2;
//            userid=userid2;
        }
        if(null==token){
             erroCode="0";
             erroMsg="无效token";
        }

        if(token!=null){
            Boolean b = checkStaticToken(token);
//        Boolean b=  userDataService.checkToken(token);
        if(!b){
            erroCode="0";
            erroMsg="无效token";
        }
        //验证token
            // 通过
            //检测用户 ,放行
            //将regCode放入cookies
            //不通过，调到某页 或者 将某个信息传到 cookies 里面
        }
        if(erroCode.equals("0")){
            erroCode="0";
            erroMsg="无效token";
            map.put("tokenErroCode",erroCode);
            map.put("tokenErroMsg",erroMsg);
            httpResponse.setCharacterEncoding("UTF-8");
            response.setContentType("text/json; charset=utf-8");
            PrintWriter out = httpResponse.getWriter();
            //输出
            out.println(JSONObject.toJSONString(map));
            out.flush();
            out.close();
            return false;
        }
        //验证通过后
        //查询本地 是否有用户
        // 如果没有就要增加用户
        System.out.println("你访问了，api接口");
        return true;
    }
    public static Boolean checkStaticToken(String token) {
        String url="http://uc.bibitrip.com:808/checkToken";
        String param="token="+token;
        String res = HttpRequestUtil.sendGet(url, param);
        JSONObject jsonObject= JSON.parseObject(res);
        String code=jsonObject.getString("code");
        if(code.equals("1")){
            return true;
        }else{
            return false;
        }
    }
}
